package com.wowkids.controller;

import com.wowkids.common.JWTUtil;
import com.wowkids.dao.UserPo;
import com.wowkids.dao.vo.ResponseVo;
import com.wowkids.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Base64;

@RestController
public class UserController {

    @Resource
    UserService userService;

    @PostMapping("/login")
    public ResponseVo login(HttpServletRequest request) {
        String usernames = request.getParameter("username");
        String password = request.getParameter("password");
        // 要先解密
        password = new String(Base64.getDecoder().decode(password));

        // 开始处理
        String loginResponse = userService.login(new UserPo(usernames, password, null, null, null));
        if (StringUtils.isNotBlank(loginResponse)) {
            return new ResponseVo(500, null, loginResponse);
        } else {
            String token = JWTUtil.encode(usernames);
            return new ResponseVo(200, token, null);
        }
    }

}
